<?php

declare(strict_types=1);

namespace app\shop\controller\admin\goods;

use think\Request;
use think\facade\Db;
use sheep\controller\traits\Crud;
use sheep\controller\Backend;
use sheep\exception\SheepException;
use app\shop\model\goods\Comment as CommentModel;
use app\shop\model\data\FakeUser as FakeUserModel;

class Comment extends Backend
{
    use Crud;

    public function initialize()
    {
        $this->model = new CommentModel;
    }

    /**
     * 商品评价列表
     *
     * @return \think\Response
     */
    public function list()
    {
        $comments = $this->model->sheepFilter()->with(['user', 'goods' => function ($query) {
            $query->field('id,image,title');
        }, 'order' => function ($query) {
            $query->removeOption('soft_delete');
        }, 'order_item'])->paginate(request()->param('list_rows', 10));

        return success('评价列表', $comments);
    }


    public function detail(Request $request, $id)
    {
        $comment = $this->model->with(['user', 'admin', 'goods' => function ($query) {
            $query->field('id,image,title,price');
        }, 'order' => function ($query) {
            $query->removeOption('soft_delete');
        }, 'order_item'])->findOrFail($id);

        return success('获取成功', $comment);
    }


    public function add(Request $request) 
    {
        $params = $this->request->only([
            'goods_id', 'user_id', 'level', 'content', 'images', 'status'
        ]);
        $params['user_type'] = 'fake_user';
        $this->svalidate($params, ".add");
        $fakeUser = FakeUserModel::find($params['user_id']);
        $params['user_nickname'] = $fakeUser ? $fakeUser->nickname : null;
        $params['user_avatar'] = $fakeUser ? $fakeUser->avatar : null;

        Db::transaction(function () use ($params) {
            $this->model->save($params);
        });
        return success('保存成功');
    }


    public function edit(Request $request, $id) 
    {
        $params = $request->only([
            'status'
        ]);

        Db::transaction(function () use ($id, $params) {
            foreach ($this->model->whereIn('id', $id)->cursor() as $comment) {
                $comment->status = $params['status'] ?? 'hidden';
                $comment->save();
            }
        });
        return success('更新成功');
    }


    public function reply(Request $request, $id)
    {
        $admin = $this->auth()->user();

        $params = $request->only([
            'content'
        ]);
        $this->svalidate($params, '.reply');

        $comment = $this->model->noReply()->findOrFail($id);

        $comment->reply_content = $params['content'];
        $comment->reply_time = time();
        $comment->admin_id = $admin->id;
        $comment->save();

        return success('回复成功');
    }

    
    /**
     * 删除商品评价
     *
     * @param  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        Db::transaction(function () use ($id) {
            foreach ($this->model->whereIn('id', $id)->cursor() as $comment) {
                $comment->delete();
            }
        });

        return success('删除成功');
    }



    /**
     * 评价回收站
     *
     * @return void
     */
    public function recyclebin() 
    {
        $comment = $this->model->onlyTrashed()->sheepFilter()->paginate(request()->param('list_rows', 10));

        return success('获取成功', $comment);
    }


    /**
     * 还原(支持批量)
     *
     * @param  $id
     * @return \think\Response
     */
    public function restore($id)
    {

        Db::transaction(function () use ($id) {
            foreach ($this->model->onlyTrashed()->whereIn('id', $id)->cursor() as $comment) {
                $comment->restore();
            }
        });

        return success('还原成功');
    }

}
